package com.why.blog.common.utils;


import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * 工具类
 */
public class Tools {
    //时间转换格式
//public static void main(String[] args) {
//	String twoTime = getTwoTime();
//	System.out.println(twoTime);
//}

		/**
		 * 自动生成MD5随机号
		 * @return
		 */
		public static  String getTime(){
			Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
			int year = c.get(Calendar.YEAR); 
			int month = c.get(Calendar.MONTH); 
			int date = c.get(Calendar.DATE); 
			int hour = c.get(Calendar.HOUR_OF_DAY); 
			int minute = c.get(Calendar.MINUTE); 
			int second = c.get(Calendar.SECOND); 
			String ti = ""+year+month+date+hour+minute+second;
			return ti;
		}

    /**
     * 自动生成Base64
     */
    public static String getSecret(){
    	
    	//int num = (int) (Math.random()*900000000+1000000000); 
    	
    	String uuid = Tools.uuid();
    	String nums = Base64.getEncoder().encodeToString(uuid.getBytes(Charset.forName("UTF-8")));//加密
    	return nums;
    }

    /**
     * String 转Date
     * @param str 传入字符串时间格式
     * @param format 时间格式
     * @return
     */
    public static Date getDate(String str,String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);//时间格式
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * Date 转String
     * @param date 传入时间
     * @param format
     * @return
     */
    public static String getDateStr(Date date,String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);//时间的格式
        String  strDate = sdf.format(date);
        return strDate;
    }

    /**
     * String 转时间戳
     * @param str 传入的时间
     * @return
     */
    public static Integer Timestamps(String str,String format){
        //sting转date
        Date date = getDate(str,format);
        Long beginUseTime = date.getTime()/1000;
        return  beginUseTime.intValue();
    }

    /**
     *
     * @param date 传入时间
     * @return
     */
    public static Integer TimestampDate(Date date) {
        Long beginUseTime = date.getTime()/1000;
        return  beginUseTime.intValue();
    }
    /**
     * 时间戳转日期
     * @param ms
     * @return
     */
    public static Date transForDate(Integer ms){
        if(ms==null){
            ms=0;
        }
        long msl=(long)ms*1000;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date temp=null;
        if(ms!=null){
            try {
                String str=sdf.format(msl);
                temp=sdf.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return temp;
    }
    /**
     * 自动生成id
     */
    public static String uuid(){
        UUID rid = UUID.randomUUID();
        String s =rid.toString();
        return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
    }
    /**
     * 获取某个时间后30天时间
     */
    public static String getTimeOut(Date data) {
        SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");  
        String maxDateStr = getDateStr(data,"yyyy-MM-dd");
        String minDateStr = ""; 
        String date ="";
        Calendar calc =Calendar.getInstance();  
        try {  
            calc.setTime(sdf.parse(maxDateStr));  
            calc.add(calc.DATE, +30);  
            Date minDate = calc.getTime();  
            minDateStr = sdf.format(minDate);
            date = minDateStr;
        } catch (ParseException e1) {  
            e1.printStackTrace();  
        }  
        return date;
    }
    
    public static String getTwoTime(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    	Date date=new Date();
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);
    	calendar.add(Calendar.DAY_OF_MONTH, -2);
    	date = calendar.getTime();
    	String format = sdf.format(date);
//    	System.out.println(format);
    	return format;
    }
    
    public static Long getDayDate(Date date,int day){
    	Calendar calendar2 = Calendar.getInstance();
    	calendar2.setTime(date);
    	calendar2.add(Calendar.DATE, day);
    	return calendar2.getTimeInMillis();
    }


    
}
